package com.hz.cheetah.client;

import com.hz.cheetah.common.bean.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by yangchuan on 16/6/30.
 */
public class ChannelHandler extends SimpleChannelInboundHandler<RpcResponse> {
    private static final Logger LOGGER = LoggerFactory.getLogger(CheetahClient.class);

    private String address;

    public ChannelHandler(String address){
        this.address = address;
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, RpcResponse response) throws Exception {
        CheetahClient client = ClientManager.channelMap.get(address);
        if(client != null){
            client.resetLasttime();
        }
        if(response.getType() == 1){
            //心跳消息
        }else {
            DefaultFuture.received(response);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        LOGGER.error("api caught exception", cause);
        ctx.close();
    }
}